// UVa1626 Brackets sequence
#include <bits/stdc++.h>
using namespace std;
#define _for(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long LL;

const int MAXN = 100 + 4, INF = 0x7f7f7f7f;
char S[MAXN];
map<char, string> M2;
inline bool match(char c1, char c2) {
  return (c1 == '[' && c2 == ']') || (c1 == '(' && c2 == ')');
}
int D[MAXN][MAXN], N;
void init_dp() {  // (i, j) <- i+1, j-1, i+1, j-1, (i,k + k+1,j)
  memset(D, 0, sizeof(D));
  _for(i, 0, N) D[i][i] = 2;
  for (int i = N - 2; i >= 0; i--) {
    _for(j, i + 1, N) {
      int &d = D[i][j];
      d = INF;
      if (match(S[i], S[j])) d = min(d, 2 + D[i + 1][j - 1]);
      _for(m, i, j) d = min(d, D[i][m] + D[m + 1][j]);
    }
  }
}

void print(int i, int j) {
  if (i > j) return;
  char cl = S[i], cr = S[j];
  if (i == j) {
    cout << M2[cl];
    return;
  }
  int d = D[i][j];
  if (match(cl, cr) && d == D[i + 1][j - 1] + 2) {
    cout << cl, print(i + 1, j - 1), cout << cr;
    return;
  }
  _for(m, i, j) if (d == D[i][m] + D[m + 1][j]) {
    print(i, m), print(m + 1, j);
    break;
  }
}
int main() {
  int T;
  M2['['] = "[]", M2['('] = "()", M2[']'] = "[]", M2[')'] = "()";
  fgets(S, MAXN, stdin);
  sscanf(S, "%d", &T);
  _for(t, 0, T) {
    if (t) puts("");
    fgets(S, MAXN, stdin), fgets(S, MAXN, stdin);
    N = strlen(S) - 1, init_dp();
    print(0, N - 1);
    cout << endl;
  }
  return 0;
}
/*
算法分析请参考: 《入门经典-第2版》 例题 9-10
注意本题中对于ostream&类型的使用
*/
// Accepted 430ms 1574 C++ 5.3.0 2020-12-08 20:42:17 25825852